var expert_common_alert_ok = "操作成功!";
/**
 * 全选或取消全选
 * 
 * @param {}
 *            ele
 */
function checkAll(ele) {
	if (ele.checked) {// 全选
		$("input[name='checkboxs']").each(function() {
			$(this).attr('checked', true);
		})
	} else {// 取消全选
		$("input[name='checkboxs']").each(function() {
			$(this).attr('checked', false);
			;
		})
	}
}
/**
 * 刷新当前页
 */
function refreshCurrentPage() {
	// 获取当前的页数
	var curPage = $(".guide").find("font").find("b").html();
	pageparam.currentpage = curPage;
	pageparam.isRefreshPage = true;
	querypage(pageparam);
	if($("#checkAllCheckBox")[0] && $("#checkAllCheckBox")[0].checked){
		$("#checkAllCheckBox").trigger("click");
	}
}
/**
 * 创建一个分页用的params
 * 
 * @param {}
 *            dataurl
 * @param {}
 *            pageurl
 * @param {}
 *            pageSize
 */
function createPageParam(dataurl, pageurl, pageSize) {
	var pageparam = new Object();
	pageparam.result = "result";
	pageparam.bar = "page_str";
	pageparam.currentpage = 1;
	pageparam.dataurl = dataurl;
	pageparam.pageurl = pageurl;
	pageparam.data = new Object();
	pageparam.data["pageSize"] = pageSize;
	pageparam.isRefreshPage = false;
	pageparam.callback = function() {
		paramsPageCallBack();
	};
	return pageparam;
}
var tempTops = 0;
jQuery(window).scroll(actionEvent);
/**
 * 监听滚动条，改变弹出框的位置
 */
function actionEvent() {
	if ($("#publish_info")) {
		if ($("#publish_info").css("display") != "none") {
			_windowScrollTop = jQuery(window).scrollTop(); // 获取当前滚动条高度
			var topScroll = -320;
			position = $(this).parent().position();
			var top = 0;
			if(position && position.top){
				top = position.top + topScroll;
			}
			tempTops = top + 340;
			$("#publish_info").css({
				"top" : tempTops
			}).show();
		}
	}
}
/**
 * 分页的回调函数
 */
function paramsPageCallBack() {
	$(".viewReason").click(function() {
		var pos = $(this).offset();
		var vals = $(this).find("font").html();
		$("#info_content").html(vals == "" || vals == " " ? "无" : vals);
		$("#publish_info").css({
			"top" : pos.top + 20,
			"left" : pos.left - 200
		}).show();
	});

	var value = $("#page_total").get(0).value;
	$("#totalCount").html(value);
	if (pageparam.isRefreshPage == true) {
		var url1 = "../page/queryCommonPage2.action";
		var obj = {
			"pageSize" : $("#page_pageSize").get(0).value,
			"currentPage" : pageparam.currentpage,
			"totalRows" : $("#page_total").get(0).value
		};
		$.post(url1, obj, function(response) {
			document.getElementById(bardiv).innerHTML = response;
			pageparam.isRefreshPage = false;

		});
	}
	// 再去重置当前页 的头部计数
	$.post("renderTitleTotalCount.action", {}, function(str) {
		resetTitleCount(str);
	});
}

/**
 * 设置头部各个类型的数量
 * 
 * @param {}
 *            str
 */
function resetTitleCount(str) {
	var titleCount = eval("(" + str + ")");
	$("#noAnswer").html(
			$("#noAnswer").html().replace(/\([0-9]*\)/,
					"(" + titleCount.noAnswer + ")"));
	$("#waitAudit").html(
			$("#waitAudit").html().replace(/\([0-9]*\)/,
					"(" + titleCount.waitAudit + ")"));
	$("#failAudit").html(
			$("#failAudit").html().replace(/\([0-9]*\)/,
					"(" + titleCount.failAudit + ")"));
	$("#publish").html(
			$("#publish").html().replace(/\([0-9]*\)/,
					"(" + titleCount.publish + ")"));
	$("#rubbish").html(
			$("#rubbish").html().replace(/\([0-9]*\)/,
					"(" + titleCount.rubbish + ")"));
	$("#expert").html(
			$("#expert").html().replace(/\([0-9]*\)/,
					"(" + titleCount.expert + ")"));
}
/**
 * 根据查询条件，重置参数
 */
function resetParams() {
	if (document.getElementById("user_id")) {
		pageparam.data["userId"] = document.getElementById("user_id").value;
	}
	if (document.getElementById("expert_id")) {
		pageparam.data["expertId"] = document.getElementById("expert_id").value;
	}
	if (document.getElementById("startdate")) {
		pageparam.data["startDate"] = document.getElementById("startdate").value;
	}
	if (document.getElementById("enddate")) {
		pageparam.data["endDate"] = document.getElementById("enddate").value;
	}
	if (document.getElementById("states")) {
		pageparam.data["states"] = document.getElementById("states").value;
	}
	if (document.getElementById("audit_id")) {
		pageparam.data["auditId"] = document.getElementById("audit_id").value;
	}
}
/**
 * 点击搜索按钮
 */
function search() {
	pageparam.currentpage = 1;
	pageparam.isRefreshPage = true;
	resetParams();
	dopage(pageparam);
};

/**
 * 在列表中点击用户名进行搜索
 */
function searchUser(userId) {
	if (document.getElementById("startdate")) {
		document.getElementById("startdate").value = "";
	}
	if (document.getElementById("enddate")) {
		document.getElementById("enddate").value = "";
	}
	if (document.getElementById("user_id")) {
		document.getElementById("user_id").value = userId;
	}
	if (document.getElementById("expert_id")) {
		document.getElementById("expert_id").value = "";
	}
	if (document.getElementById("audit_id")) {
		document.getElementById("audit_id").value = "";
	}
	search();
}

/**
 * 在列表中点击顾问名进行搜索
 */
function searchExpert(expertId) {
	if (document.getElementById("startdate")) {
		document.getElementById("startdate").value = "";
	}
	if (document.getElementById("enddate")) {
		document.getElementById("enddate").value = "";
	}
	if (document.getElementById("user_id")) {
		document.getElementById("user_id").value = "";
	}
	if (document.getElementById("expert_id")) {
		document.getElementById("expert_id").value = expertId;
	}
	if (document.getElementById("audit_id")) {
		document.getElementById("audit_id").value = "";
	}
	search();
}

/**
 * 在列表中点击审核人进行搜索
 */
function searchAudit(auditId) {
	if (document.getElementById("startdate")) {
		document.getElementById("startdate").value = "";
	}
	if (document.getElementById("enddate")) {
		document.getElementById("enddate").value = "";
	}
	if (document.getElementById("user_id")) {
		document.getElementById("user_id").value = "";
	}
	if (document.getElementById("expert_id")) {
		document.getElementById("expert_id").value = "";
	}
	if (document.getElementById("audit_id")) {
		document.getElementById("audit_id").value = auditId;
	}
	search();
}

/**
 * 点击回答问题(未答复模块) 或 点击编辑（未通过模块）或 点击答复（垃圾箱模块）
 */
function answerQuestion(qId, ele, flag) {
	resetParams();
	var className = $(ele).attr("class");
	if (/none/.test(className)) {
		return;
	} else {
		$.post("editAnswerQuestion.action", {
			questionId : qId
		}, function(data) {
			// alert(data);
			clearExpertRefreshInterval();
			data = eval("(" + data + ")");
			if (data.state != 0) {
				alert(data.msg);
				refreshCurrentPage();
			} else {
				var parentLi = $(ele).parent().parent().parent();
				if (!/li/gim.test(parentLi.get(0).tagName)) {
					parentLi = parentLi.parent();
				}
				parentLi.removeClass("cur2").addClass("cur1");
				var parentSpan = $(ele).parent();
				parentSpan.find("a").each(function() {
					$(this).addClass("none");
				})
				parentLi.find("#state").html("状态( 我 正在编辑)");
				// 打开富文本
				addeditor(parentLi.find('#content'));
				parentLi.find('#tables').show();
				parentLi.find('#expert_answer').hide();
				if (flag) {
					parentLi.find(".answer").show();
				}
			}
		});
	}
}

/**
 * 还原问题
 */
function revertQuestion(qId, ele, answer) {
	resetParams();
	var className = $(ele).attr("class");
	var parentLi = $(ele).parent().parent().parent();
	if (/none/.test(className)) {
		return;
	} else {
		$.post("revertQuestion.action", {
			questionIdGroup : qId,
			answer : parentLi.find("#expert_answer").html()
		}, function(data) {
			data = eval("(" + data + ")");
			if (data.state != 0) {
				alert(data.msg);
			} else {
				alert(expert_common_alert_ok);
			}
			refreshCurrentPage();
		});
	}
}
/**
 * 提交答案
 */
function submitAnswer(qId, ele, alertInfo) {
	var answers = $(ele).parent().parent().find("#content").val();
	var v = $.trim(answers);
	if (v == null || v == "" || v == "<p>&nbsp;</p>") {
		alert("请填写答案");
		return;
	}
	$.post("answerQuestion.action", {
		questionId : qId,
		answer : answers
	}, function(data) {
	    genderExpertRefreshInterval();
		data = eval("(" + data + ")");
		if (data.state == 0) {
			if (!alertInfo) {
				alertInfo = expert_common_alert_ok;
			}
			alert(alertInfo);
			refreshCurrentPage();
		} else {
			alert(data.msg);
			refreshCurrentPage();
		}
	});
}

/**
 * 移除问题
 */
function removeQuestion(idStr, ele) {
	if ($(ele).hasClass("none")) {
		return;
	}
	var isSelected = false;
	if (!idStr) {
		isSelected = true;
		var arr = $("input[name='checkboxs']:checked");
		if (arr.size() == 0) {
			alert("请选择要移除的条目");
			return;
		}
		var valArr = new Array();
		for ( var i = 0; i < arr.size(); i++) {
			valArr.push(arr.eq(i).attr("value"));
		}
		idStr = valArr.join(",");
	}

	$.post("removeQuestion.action", {
		questionIdGroup : idStr
	}, function(data) {
		data = eval("(" + data + ")");
		if (!isSelected) {
		}
		if (data.state == 0) {
			alert(expert_common_alert_ok);
			refreshCurrentPage();
		} else {
			alert(data.msg);
			refreshCurrentPage();
		}
		if ($("#publish_info")) {
			$("#publish_info").hide();
		}
	});
}

/**
 * 取消答复
 */
function cancelAnswer(qId, ele) {
	var className = $(ele).attr("class");
	if (/none/.test(className)) {
		return;
	} else {
		$.post("cancelQuestion.action", {
			questionId : qId
		}, function(data) {
			genderExpertRefreshInterval();
			data = eval("(" + data + ")");
			if (data.state != 0) {
				refreshCurrentPage();
			} else {
				refreshCurrentPage();
			}
		});
	}
}
/**
 * 删除问题
 */
function delQuestion(qId) {
	$.post("delQuestion.action", {
		questionIdGroup : qId
	}, function(data) {
		data = eval("(" + data + ")");
		if (data.state != 0) {
			var msg = data.msg;
			if(!msg){
				msg = "操作失败，请稍后再试！";
			}
			alert(msg);
			refreshCurrentPage();
		} else {
			refreshCurrentPage();
		}
	});

}

/**
 * 发布已审核的投顾提示
 */
function publishExpertAlert(qId, ele) {
	if (confirm("确定要发布吗？")) {
		resetParams();
		$.post("auditQuestion.action", {
			questionIdGroup : qId
		}, function(data) {
			// alert(data);
			data = eval("(" + data + ")");
			if (data.state != 0) {
				alert(data.msg);
			} else {
				alert(expert_common_alert_ok);
			}
			refreshCurrentPage();
		});
	}
}
/**
 * 预览问题
 */
function viewQuestion(ele, question, user_id) {
	var li = $(ele).parent().parent().parent();
	var answer = li.find("#content").val();
	window.open(encodeURI(encodeURI('../../expert/view.jsp?answer=' + answer
			+ "&question=" + question + "&user_id=" + user_id)), "newwindow",
			"top=0,left=0, status=no");
}
